//
//  DailyHeader.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/25.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "DailyHeader.h"
#import "ShowImageView.h"
#import "ArticleModel.h"
#import "Define.h"

#import "UIImageView+WebCache.h"

#import "NightMode.h"

@interface DailyHeader () <UIScrollViewDelegate,ShowImageViewDlegate>
@property (nonatomic,retain) UIScrollView * upScrollView;
@property (nonatomic,retain) UIPageControl * page;
@property (nonatomic,retain) UIScrollView * downScrollView;
@end

@implementation DailyHeader
-(void)dealloc{
    [_upScrollView release];
    [_page release];
    [_dateLabel release];
    [_titleLabel release];
    [_downScrollView release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createDateViews];
        [self createUpScroll];
        [self createDownScroll];
    }
    return  self;
}
-(void)createDateViews{
    // 创建日期view
    UIView * dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 70)];
    [dateView setBackgroundColor:[UIColor clearColor]];
    [self addSubview:dateView];
    [dateView release];
    
    // 黄色的小竖杠
    UIView * yello = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, dateView.frame.size.height)];
    yello.backgroundColor = [UIColor colorWithRed:247/255.0 green:194/255.0 blue:40/255.0 alpha:1];
    [dateView addSubview:yello];
    [yello release];
    
    // 显示日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, dateView.frame.size.height)];
    _dateLabel.font = [UIFont boldSystemFontOfSize:30];
    _dateLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
    _dateLabel.textAlignment = NSTextAlignmentCenter;
    [dateView addSubview:_dateLabel];
    [_dateLabel release];
    
    // 显示标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(yello.frame.size.width + _dateLabel.frame.size.width+10, 0, dateView.frame.size.width - 10 - 90 - 20, dateView.frame.size.height)];
    [self addSubview:_titleLabel];
    _titleLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [_titleLabel release];
}
// 上面的scrollView为只有两张图片的轮播图，一张一张地滚
-(void)createUpScroll{
    self.upScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _dateLabel.frame.size.height+1, self.frame.size.width, self.frame.size.width/2)]; // 图片的真正宽高比为2：1
    _upScrollView.tag = 1001; // 区分scrollView
    _upScrollView.pagingEnabled = YES;
    _upScrollView.contentSize = CGSizeMake(self.frame.size.width*4, 0);
    _upScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    _upScrollView.bounces = NO;
    _upScrollView.showsHorizontalScrollIndicator = NO;
    _upScrollView.delegate = self;
    [self addSubview:_upScrollView];
    _upScrollView.backgroundColor = [UIColor lightGrayColor];
    [_upScrollView release];
    
    // 创建pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width-40, _dateLabel.frame.size.height + _upScrollView.frame.size.height-30, 40, 30)];
    [self addSubview:_page]; // 要加到self上
    _page.numberOfPages = 2;
    _page.pageIndicatorTintColor = [UIColor grayColor];
    _page.currentPageIndicatorTintColor = [UIColor whiteColor];
    [_page release];
}
// 下面的scrollView为多张图片的轮播图
-(void)createDownScroll{
    self.downScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _dateLabel.frame.size.height + _upScrollView.frame.size.height, self.frame.size.width, 190)];
    _downScrollView.tag = 1002;
    _downScrollView.bounces = NO;
    _downScrollView.delegate = self;
    
    [self addSubview:_downScrollView];
    [_downScrollView release];
}

#pragma mark - 数据发送过来，创建上下两个scrollView
-(void)setHeaderWithDic:(NSDictionary *)dic modelArray:(NSArray *)array{
    _dateLabel.text = [dic objectForKey:@"showtext"];
    _titleLabel.text = [dic objectForKey:@"topic"];
    [self createUpImageViewsWithArray:array];
    [self createDownImageViewWithArray:array];
}
#pragma mark - 创建上面的轮播图，有两张图片
-(void)createUpImageViewsWithArray:(NSArray *)array{
    ArticleModel * model1 = [array objectAtIndex:1];
    ShowImageView * image1 = [[ShowImageView alloc] initWithFrame:CGRectMake(0, 0, _upScrollView.frame.size.width, _upScrollView.frame.size.height) model:model1];
    image1.delegate = self;
    NSDictionary * simgDic1 = [model1.simgs objectAtIndex:0]; // 取得simg数组的唯一一个元素，储存图片的信息
    NSString * urlString1 = [NSString stringWithFormat:@"%@%@",kWebImageDomain,[simgDic1 objectForKeyedSubscript:@"url"]];
    [image1 sd_setImageWithURL:[NSURL URLWithString:urlString1]];
    [_upScrollView addSubview:image1];
    [image1 release];
    
    ArticleModel * model2 = [array objectAtIndex:0];
    ShowImageView * image2 = [[ShowImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, _upScrollView.frame.size.width, _upScrollView.frame.size.height) model:model2];
    image2.delegate = self;
    NSDictionary * simgDic2 = [model2.simgs objectAtIndex:0]; // 取得simg数组的唯一一个元素，储存图片的信息
    NSString * urlString2 = [NSString stringWithFormat:@"%@%@",kWebImageDomain,[simgDic2 objectForKeyedSubscript:@"url"]];
    [image2 sd_setImageWithURL:[NSURL URLWithString:urlString2]];
    [_upScrollView addSubview:image2];
    [image2 release];
    
    ArticleModel * model3 = [array objectAtIndex:1];
    ShowImageView * image3 = [[ShowImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*2, 0, _upScrollView.frame.size.width, _upScrollView.frame.size.height) model:model3];
    image3.delegate = self;
    NSDictionary * simgDic3 = [model3.simgs objectAtIndex:0]; // 取得simg数组的唯一一个元素，储存图片的信息
    NSString * urlString3 = [NSString stringWithFormat:@"%@%@",kWebImageDomain,[simgDic3 objectForKeyedSubscript:@"url"]];
    [image3 sd_setImageWithURL:[NSURL URLWithString:urlString3]];
    [_upScrollView addSubview:image3];
    [image3 release];
    
    ArticleModel * model4 = [array objectAtIndex:0];
    ShowImageView * image4 = [[ShowImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*3, 0, _upScrollView.frame.size.width, _upScrollView.frame.size.height) model:model4];
    image4.delegate = self;
    NSDictionary * simgDic4 = [model4.simgs objectAtIndex:0]; // 取得simg数组的唯一一个元素，储存图片的信息
    NSString * urlString4 = [NSString stringWithFormat:@"%@%@",kWebImageDomain,[simgDic4 objectForKeyedSubscript:@"url"]];
    [image4 sd_setImageWithURL:[NSURL URLWithString:urlString4]];
    [_upScrollView addSubview:image4];
    [image4 release];
    
    // 创建定时器
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(upTimerAction) userInfo:nil repeats:YES];
}

#pragma  mark - 创建下面的scrollView，有6张图片
-(void)createDownImageViewWithArray:(NSArray *)array{
    float xOffset = 0; // 计算总宽度

    // 图片从2-8
    for(int i=2;i<8;i++){
        
        ArticleModel * model = [array objectAtIndex:i];
        NSDictionary * simgDic = [model.simgs objectAtIndex:0];
        // 计算出宽高比例
        NSNumber * height = [simgDic objectForKey:@"height"];
        NSNumber * width = [simgDic objectForKey:@"width"];
        float scale = [width floatValue] / [height floatValue];
        float widthInScreen = (_downScrollView.frame.size.height-52)*scale;
        

        UIImageView * filmImage = [[UIImageView alloc] initWithFrame:CGRectMake(
                                                                  xOffset,
                                                                  0,
                                                                  widthInScreen,
                                                                  _downScrollView.frame.size.height)];
        ShowImageView * image = [[ShowImageView alloc] initWithFrame:CGRectMake(
                                                                xOffset+1,
                                                                26,
                                                                widthInScreen+1,
                                                                _downScrollView.frame.size.height - 52
                                                                ) model:model];
        image.delegate = self;

        // 如果小于1/4，使用第一个小的胶卷图片；如果在1/4和1/2之间，使用第二张图片，如果大于1/2；使用第三张图片
        if(widthInScreen<self.frame.size.width/3){
            filmImage.image = [UIImage imageNamed:@"film.png"];
        }
        else if (widthInScreen >= self.frame.size.width/3 && widthInScreen < self.frame.size.width /2){
            filmImage.image = [UIImage imageNamed:@"film2.png"];
        }
        else
            filmImage.image = [UIImage imageNamed:@"film3.png"];
        
        [_downScrollView addSubview:filmImage];
        [filmImage release];
        

        NSString * urlString = [NSString stringWithFormat:@"%@%@",kWebImageDomain,[simgDic objectForKeyedSubscript:@"url"]];
        [image sd_setImageWithURL:[NSURL URLWithString:urlString]];
        [_downScrollView addSubview:image];
        
        [image release];
        xOffset += widthInScreen;
        _downScrollView.contentSize = CGSizeMake(xOffset, 0);
    }
}

#pragma  mark -  ScrollView协议滚动完成
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView.tag == 1001){
        CGPoint point = scrollView.contentOffset;
        if(point.x == scrollView.frame.size.width*3){
            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
        }
        if(point.x == 0){
            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width*2, 0);
        }
        
        int index = _upScrollView.contentOffset.x/self.frame.size.width - 1;
        if(index == -1) index = 1;
        else if(index == 2) index = 0;
        
        _page.currentPage = index;
    }
}
// 上面的scrollView滚动
-(void)upTimerAction{
    [UIView animateWithDuration:0.5 animations:^{
        _upScrollView.contentOffset = CGPointMake(_upScrollView.contentOffset.x+self.frame.size.width, 0);
    } completion:^(BOOL finished) {
        int index = _upScrollView.contentOffset.x/self.frame.size.width - 1;
        if(index == -1) index = 1;
        else if(index == 2) index = 0;
        _page.currentPage = index;
        
        if(_upScrollView.contentOffset.x == self.frame.size.width * 3)
            _upScrollView.contentOffset = CGPointMake(_upScrollView.frame.size.width, 0);
    }];
}

#pragma mark - ShowImageView 的协议
-(void)enterDetailWithModel:(ArticleModel *)model{
    [self.delegate enterDetailWithModel:model];
}
@end
